/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import entidades.CambioDeRepuestos;
import entidades.Eje;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import vistas.FormCambioDeRepuestos;

/**
 *
 * @author Pablo
 */
public class DialogSeleccionarRueda extends javax.swing.JDialog {

    SeleccionarRepuesto sel_repuesto;
    int fila, columna;

    public DialogSeleccionarRueda(java.awt.Frame parent, boolean modal, SeleccionarRepuesto sel_repuesto) {
        super(parent, modal);
        initComponents();
        this.sel_repuesto = sel_repuesto;
        crearConfiguracion();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        tf_rueda_selecionada = new javax.swing.JTextField();
        bt_aceptar = new javax.swing.JButton();
        bt_cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabla.setRowSelectionAllowed(false);
        tabla.setShowHorizontalLines(false);
        tabla.setShowVerticalLines(false);
        jScrollPane1.setViewportView(tabla);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Rueda seleccionada:");

        tf_rueda_selecionada.setEditable(false);
        tf_rueda_selecionada.setFont(new java.awt.Font("Tahoma", 2, 12)); // NOI18N

        bt_aceptar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_aceptar.setText("Aceptar");
        bt_aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_aceptarActionPerformed(evt);
            }
        });

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(bt_aceptar)
                                .addGap(67, 67, 67)
                                .addComponent(bt_cancelar))
                            .addComponent(tf_rueda_selecionada))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_rueda_selecionada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_aceptar)
                    .addComponent(bt_cancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_aceptarActionPerformed
        this.sel_repuesto.setRuedaSelecionarda(fila, columna);
        this.dispose();
    }//GEN-LAST:event_bt_aceptarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_aceptar;
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField tf_rueda_selecionada;
    // End of variables declaration//GEN-END:variables

    DefaultTableModel modelo_tabla;

    private void crearConfiguracion() {
        iniciarModeloTabla();
    }

    JButton bt2;

    private void iniciarModeloTabla() {
        // Estos son los tipos de datos de cada columna de la lista
        String[] columnas = new String[]{
            "",
            "",
            "",
            "",
            "",
            "",
            ""};

        final Class[] columnNames = new Class[]{
            JButton.class,
            JButton.class,
            java.lang.String.class,
            java.lang.String.class,
            JButton.class,
            JButton.class,
            int.class
        };

        JButton bt = new JButton("");
        bt.setBackground(Color.WHITE);
        bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/wheel_icon.jpg")));
        bt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int fila = tabla.getSelectedRow();
                int col = tabla.getSelectedColumn();
                tf_rueda_selecionada.setText(fila + ":" + col);
            }
        });

        bt2 = new JButton("");
        bt2.setBackground(Color.WHITE);
        bt2.setEnabled(true);

        Object[][] datos = new Object[][]{};

        tabla.setModel(new javax.swing.table.DefaultTableModel(datos, columnas) {
            Class[] tipos = columnNames;

            @Override
            public Class getColumnClass(int columnIndex) {
                return tipos[columnIndex];
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                return !(this.getColumnClass(column).equals(JButton.class));
            }
        });

        tabla.setDefaultRenderer(JButton.class, new TableCellRenderer() {
            public Component getTableCellRendererComponent(JTable jtable, Object objeto, boolean estaSeleccionado, boolean tieneElFoco, int fila, int columna) {
                return (Component) objeto;
            }
        });
        tabla.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                fila = tabla.rowAtPoint(e.getPoint());
                columna = tabla.columnAtPoint(e.getPoint());
                if (tabla.getModel().getColumnClass(columna).equals(JButton.class)) {
                    JButton bt_selected = (JButton) tabla.getValueAt(fila, columna);
                    if (bt_selected != bt2) {

                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < tabla.getModel().getColumnCount(); i++) {
                            if (!tabla.getModel().getColumnClass(i).equals(JButton.class)) {
                                sb.append("\n").append(tabla.getModel().getColumnName(i)).append(": ").append(tabla.getModel().getValueAt(fila, i));
                            }
                        }                                              
                        
                        if (columna <= 1){                            
                            columna++;
                        }                            
                        else{
                            int nro_ruedas = (int)tabla.getValueAt(fila, 6);                            
                            if(nro_ruedas == 2){
                                columna -=3;
                            }
                            else
                                columna--;
                        }
                        fila++;
                                                 
                            
                        tf_rueda_selecionada.setText("Eje: " + fila + " - Rueda: " + columna);
                    }
                }
            }
        });

        modelo_tabla = (DefaultTableModel) tabla.getModel();

        List<Eje> ls_ejes = FormCambioDeRepuestos.vehiculo_selected.getConfiguracionEjes().getLs_ejes();
        int nro_ruedas;
        Object f[];
        for (Eje e : ls_ejes) {
            nro_ruedas = e.getNro_ruedas();
            if (nro_ruedas == 2) {                
                f = new Object[]{bt, bt2, " ", " ", bt2, bt, 2}; //Fila vacia  
                modelo_tabla.addRow(f);
            } else {
                f = new Object[]{bt, bt, " ", " ", bt, bt, 4}; //Fila vacia  
                modelo_tabla.addRow(f);
            }
        }

        tabla.setRowHeight(60);

    }


}
